import './css/normalize.css';
import './css/main.css';
import { eventBus } from './bootstrap/initEventBus';
import { initKeyBar } from './component/keyboard';
import { translateEnterKind } from './bootstrap/translateCenter';
import { handleInput } from './bootstrap/initEditorElement';
const keyboard = initKeyBar();
const {Tips, Formula, Result} = eventBus.store;

// 禁止文本被选中
document.addEventListener('selectstart', function (evt) {
    evt.preventDefault();
})

// 获取页面元素
window.addEventListener('DOMContentLoaded', () => {
    const TopContain: Element | null = document.querySelector('.show')
    const keyEnter: Element | null = document.querySelector('.keyEnter');

    if (TopContain) {
        TopContain.appendChild(Tips);
        TopContain.appendChild(Formula);
        TopContain.appendChild(Result);
    }

    keyboard.addEventListener('click', handelKeyBoardClick);
    keyEnter?.appendChild(keyboard);
});

function handelKeyBoardClick(event: MouseEvent) {
    let keyEle = event.target as HTMLElement; // 事件源
    if (keyEle) {
        let tagName = keyEle.nodeName.toLowerCase(); // 事件源的标签类型
        // 只有点击到li标签才继续执行代码，若事件源是li中的span，把事件源给回li
        switch (tagName) {
            case 'li':
                break;
            case 'span':
                keyEle = keyEle.parentNode as HTMLElement;
                break;
            default:
                return false;
        }
        let TypeIndex = keyEle.dataset.type;
        if (!TypeIndex) {
            return;
        }
        let enterItem = translateEnterKind(TypeIndex);
        if (enterItem) handleInput(enterItem)
    }
}


